<template>
  <div>
   <el-card class="box-card">
    <el-form :inline="true" :model="formInline">
  <el-form-item label="目录名称">
    <el-input></el-input>
  </el-form-item>
  <el-form-item label="状态">
    <el-select v-model="form.state" placeholder="请选择">
      <el-option label="启用" :value="1"></el-option>
      <el-option label="禁用" :value="0"></el-option>
    </el-select>
  </el-form-item>
   <el-form-item>
    <el-button type="plain" @click="display()">清除</el-button>
    <el-button type="primary" @click="screen()">搜索</el-button>
  </el-form-item>
  <el-button type="success" icon="el-icon-edit"
  style='position: absolute;right:20px;top:20px;'
  @click="dialogVisible=true">新增目录</el-button>
</el-form>
 <el-alert
    type="info"
    show-icon
    :closable="false">
    数据一共{{ counts }}条
  </el-alert>
  <br>
  <el-table
      :data="taglist"
      style="width: 100%">
      <el-table-column type="index" label="序号" width="100">
      </el-table-column>
      <el-table-column prop="subjectName" label="所属学科">
      </el-table-column>
      <el-table-column prop="tagName" label="标签名称">
      </el-table-column>
      <el-table-column prop="username" label="创建者">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期">
         <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template slot-scope="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
      </el-table-column>
      <el-table-column prop="date" label="操作">
        <template #default="{row}">
          <!-- {{row}} -->
        <!-- <template v-if="show"> -->
          <el-button type="text" @click="show(row)">{{row.state===0?'禁用':'启用'}}</el-button>
          <el-button type="text" @click="redact(row.id)" :disabled='row.state===1'>修改</el-button>
          <el-button type="text" @click="delemployee(row.id)" :disabled="row.state===1">删除</el-button>
        </template>
        <!-- <template v-if="show"> -->
        <!-- <template v-else>
          <el-button type="text" @click="show">禁用</el-button>
          <el-button type="text" disabled>修改</el-button>
          <el-button type="text" disabled>删除</el-button>
        </template> -->
      </el-table-column>
    </el-table>
    <br>
    <!-- 分页 -->
  <div class="block">
    <el-pagination
      style='position: absolute;right:20px;bottom:2px;'
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="size"
      :current-page="page"
      layout="sizes, prev, pager, next, jumper"
      :total="counts"
      >
    </el-pagination>
  </div>
  <!-- 增加目录框 -->
  <el-dialog
        title="新增目录"
        :visible.sync="dialogVisible"
      >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <!-- <el-form-item label="所属学科" prop="subjectID">
            <el-input v-model="form.subjectID" placeholder="请输入内容"/>
          </el-form-item> -->
          <el-form-item label="所属学科" v-if="!$route.query.id" prop="subjectID">
          <el-select v-model="form.subjectID" style="width:100%">
            <el-option
              v-for="item in subjectOptions"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="form.tagName" placeholder="请输入目录名称"/>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="checkout">确 定</el-button>
        </span>
      </el-dialog>
  <!-- 修改目录框 -->
  <el-dialog
    title="修改目录"
      :visible.sync="disdialogVisible"
    >
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <!-- <el-form-item label="所属学科" prop="subjectID">
            <el-input v-model="form.subjectID" placeholder="请输入内容"/>
          </el-form-item> -->
          <el-form-item label="所属学科" v-if="!$route.query.id" prop="subjectID">
          <el-select v-model="form.subjectID" style="width:100%">
            <el-option
              v-for="item in subjectOptions"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="form.tagName" placeholder="请输入目录名称"/>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="disdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="discheckout">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, detail, update, changeState } from '@/api/hmmm/tags.js'
import { simple as subjectList } from '@/api/hmmm/subjects'
export default {
  name: 'TagsList',
  data () {
    return {
      formInline: {
        region: ''
      },
      size: 20,
      page: 1,
      counts: 0,
      taglist: [],
      dialogVisible: false,
      disdialogVisible: false,
      form: {
        subjectID: null,
        // directoryName: '',
        tagName: null,
        state: null,
        page: 1,
        pagesize: 20,
        id: null
      },
      // 规则
      rules: {
        subjectID: [
          { required: true, message: '请选择', trigger: ['change', 'blur'] }
          // { min: 1, message: '请不要输入空的角色名称', trigger: ['change', 'blur'] },
        ],
        directoryName: [
          { required: true, message: '请输入标签名称', trigger: ['change', 'blur'] }
          // { min: 1, message: '请不要输入空的角色名称', trigger: ['change', 'blur'] },
        ]
      },
      subjectOptions: []
      // getTagList: []
    }
  },
  async created () {
    this.getTagList()
    const { data: subjectArr } = await subjectList()
    this.subjectOptions = subjectArr
  },
  methods: {
    // 获取标签页面数据
    async getTagList () {
      const { data } = await list(this.form)
      console.log(data)
      this.taglist = data.items
      this.counts = data.counts
    },
    // 增添标签
    checkout () {
      this.$refs.form.validate(async (boo) => {
        if (!boo) return
        const res = await add(this.form)
        console.log(res)
        // 重新渲染列表
        this.getTagList()
        // 关闭对话框
        this.dialogVisible = false
        // 新增后清空对话框
        this.$refs.form.resetFields()
      })
    },
    // 点击修改回显标签信息
    async redact (id) {
      console.log(id)
      this.disdialogVisible = true
      const res = await detail({ id: id })
      console.log(res)
      // this.form = res.data
      // this.form.state = null
      this.form.subjectID = res.data.subjectID
      this.form.tagName = res.data.tagName
      // this.form.tagName = res.data.tagName
      this.form.id = res.data.id
      this.disdialogVisible = true
    },
    // 修改标签
    discheckout () {
      this.$refs.form.validate(async (boo) => {
        if (!boo) return
        const res = await update(this.form)
        console.log(res)
        // 重新渲染列表
        this.getTagList()
        // 关闭对话框
        this.disdialogVisible = false
        // 新增后清空对话框
        this.$refs.form.resetFields()
      })
    },
    // 删除标签
    delemployee (id) {
      this.$confirm('此操作将永久删除该人标签 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        // console.log(11111)
        // console.log(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getTagList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 每页条数发生变化触发的处理函数
    handleSizeChange (val) {
      // 将最新的每页条数赋值给size
      this.form.pagesize = val
      // 重新获取
      this.getTagList()
    },
    // 页码发生变化触发的处理函数
    handleCurrentChange (val) {
      // 将最新的页码赋值给page
      this.form.page = val
      console.log(val)
      // 重新获取
      this.getTagList()
    },
    // 切换状态
    async show (tag) {
      await changeState({
        id: tag.id,
        state: tag.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      tag.state = tag.state === 1 ? 0 : 1
      console.log(tag.state)
    },
    // 清除搜索框
    display () {
      this.form = {
        tagsName: null,
        state: null,
        page: 1,
        pagesize: 5
      }
    },
    // 筛选标签
    screen () {
      this.form.page = 1
      this.getTagList()
    }
  }
}
</script>

<style scoped>
.box-card {
    /* width: 480px; */
    background-color: white;
    margin-left: 10px;
    margin-right: 10px;
    margin-top: 10px;
  }
  </style>
